﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using Microsoft.AspNet.Identity;
using PublishNews.Entities;
using PublishNews.Repositories;

namespace PublishNews.Controllers
{
    [Authorize]
    public class BannerController : Controller
    {
        public IBannerRepository Repository;

        public BannerController(IBannerRepository bannerRepository)
        {
            Repository = bannerRepository;
        }

        //
        // GET: /LocationApi/

        public async Task<ActionResult> Index()
        {
            return View();
        }

        public ActionResult Create()
        {
            return View(false);
        }

        public async Task<ActionResult> Upload()
        {
            var result = false;
            if (Request.Files.Count > 0)
            {
                Uri uri = HttpContext.Request.Url;

                HttpPostedFileBase hpf = Request.Files[0];

                string extension = Path.GetExtension(hpf.FileName);
                string newFileName = Guid.NewGuid().ToString() + extension;

                if (hpf != null && hpf.ContentLength > 0)
                {
                    var path = Path.Combine(Server.MapPath("~/Images/"), newFileName);
                    hpf.SaveAs(path);

                    string url = Request.Form["url"];
                    Banner banner = new Banner()
                    {
                        ImageUrl = "/Images/" + newFileName,
                        LinkUrl = url,
                        CreatedBy = User.Identity.GetUserName(),
                        CreatedDate = DateTime.Now
                    };
                    await Repository.Add(banner);
                    if (banner.Id != Guid.Empty)
                    {
                        result = true;
                    }
                }
            }
            return View("Create", result);
        }

        public async Task<JsonResult> Delete(Guid id)
        {
            var result = await Repository.Remove(id);
            return Json(result > 0 ? true : false, JsonRequestBehavior.AllowGet);
        }

        public async Task<ActionResult> Edit(Guid id)
        {
            var banner = await Repository.GetBy(id);
            return View(banner);
        }

        public async Task<ActionResult> UpdateBanner()
        {
            Guid id = Guid.Parse(Request.Form["Id"]);
            var oldBanner = await Repository.GetBy(id);

            string url = Request.Form["url"];
            oldBanner.LinkUrl = url;

            if (Request.Files.Count > 0)
            {
                Uri uri = HttpContext.Request.Url;

                HttpPostedFileBase hpf = Request.Files[0];

                string extension = Path.GetExtension(hpf.FileName);
                string newFileName = Guid.NewGuid().ToString() + extension;



                if (hpf != null && hpf.ContentLength > 0)
                {
                    var path = Path.Combine(Server.MapPath("~/Images/"), newFileName);
                    hpf.SaveAs(path);
                    oldBanner.ImageUrl = "/Images/" + newFileName;
                }
            }

            var result = await Repository.Update(oldBanner);

            return View("Index");
        }
    }
}
